﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using RoMFramework.RomAPI;

namespace RoMFramework.Hacks
{
    public sealed class NoFall : HackBase 
    {
        private byte[] m_NoFallOriginalBytes;
        public static List<Keys> StaticHotKeys, StaticDefaultHotKeys;

        public NoFall(RomClient client) : base(client)
        {
            HackName = "NoFall";
            LoadHotKeys();
            //InitialiseHotKeys();            
            //DefaultHotKeys.Add(Keys.NumPad0);
            //HotKeys.Add(Properties.Settings.Default.NoFallHotkey);
            //HotKeys.Add(Keys.Z);
            CurrentHotKeys = new List<Keys>(HotKeys);
        }

        private static void InitialiseHotKeys()
        {
            if (StaticDefaultHotKeys == null)
            {
                StaticDefaultHotKeys = new List<Keys>();
            }
            if (StaticHotKeys == null)
            {
                StaticHotKeys = new List<Keys>();
            }
        }

        public override List<Keys> HotKeys
        {
            get { InitialiseHotKeys(); return StaticHotKeys;}
            protected set { StaticHotKeys = value; }
        }

        public override List<Keys> DefaultHotKeys
        {
            get { InitialiseHotKeys(); return StaticDefaultHotKeys; }
            protected set { StaticDefaultHotKeys = value; }
        }

        /// <summary>
        /// Enables the No Fall
        /// </summary>
        public override void Enable(Keys keys)
        {
            if (Enabled)
                return;
            m_NoFallOriginalBytes = m_Client.RoM.ReadBytes(m_Client.Pointers.PatternSearcher.AddressesDictionary["NoFall"], (int)FunctionSizes.NoFall);
            m_Client.RoM.Asm.Clear();
            for (int i = 0; i < (int)FunctionSizes.NoFall; i++)
            {
                m_Client.RoM.Asm.AddLine("nop");
            }
            var m_NoFallBytes = m_Client.RoM.Asm.Assemble();
            m_Client.RoM.WriteBytes(m_Client.Pointers.PatternSearcher.AddressesDictionary["NoFall"], m_NoFallBytes);
            Enabled = true;
        }

        /// <summary>
        /// Disables No Fall
        /// </summary>
        public override void Disable(Keys keys)
        {
            if (!Enabled)
                return;
            m_Client.RoM.WriteBytes(m_Client.Pointers.PatternSearcher.AddressesDictionary["NoFall"], m_NoFallOriginalBytes);
            Enabled = false;
        }

        /// <summary>
        /// Toggles No Fall
        /// </summary>
        public override void Toggle(Keys keys)
        {
            if (Enabled)
            {
                Disable(keys);
            }
            else if (!Enabled)
            {
                Enable(keys);
            }
        }
    }
}
